export class AssetStore {
  readonly #images = new Map<string, HTMLImageElement>();

  getImageByUrl(url: string): HTMLImageElement | null {
    return this.#images.get(url) ?? null;
  }

  async loadAllImagesByUrl(urls: string[]) {
    const responses = await Promise.all(urls.map((url) => fetch(url)));

    for (const response of responses) {
      const blob = await response.blob();
      const image = new Image();
      image.src = URL.createObjectURL(blob);

      this.#images.set(response.url, image);
    }
  }

  cleanUp() {
    for (const image of this.#images.values()) {
      URL.revokeObjectURL(image.src);
    }

    this.#images.clear();
  }
}
